浏览量 4894
2019/03/29 11:53
示例:点击 -> 性能监控
/proc/net/tcp 第四列 01代表了 TCP_ESTABLISHED 06代表代表time_wait 08代表close_wait
[root@wangzi ~]# cat /proc/net/tcp| awk '{if($4 == '01') print $0}'|wc -l
22
[root@wangzi ~]# netstat -antpl|grep ESTABLISHED|wc -l
22
[root@wangzi ~]#cat /proc/net/tcp| awk '{if($4 == '01' || $4=='06' || $4=='08') print $4}'
因使用netstat命令有时会占用较多资源,当机器负载较高时,可以用使用上面的命令来查看tcp正在通信的连接数。
附图一张:
python监控:
#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: 277215243@qq.com
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com
def net_tcp():
ret=Popen("""cat /proc/net/tcp| awk '{if($4 == '01' || $4=='06' || $4=='08') print $4}'""",shell=True,stdout=PIPE)
established=0
time_wait=0
close_wait=0
for item in ret.stdout.readlines():
if int(item)==1:
established+=1
elif int(item)==6:
time_wait+=1
elif int(item)==6:
close_wait+=1
insert_sql="""insert into `monitor_net_tcp` (`establish`,`time_wait`,`close_wait`,`flow_time`) values ({0},{1},{2},'{3}')""".format(established,time_wait,close_wait,now_zero)
cursorUpdate(insert_sql,[])
上一篇 搜索 下一篇